package com.operation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.db.domain.Communication;
import com.xml.RecordExtract;

/**
 * 数据同步
 * 
 * @author Administrator
 * 
 */
public class TreeTool {
	public Map<String, Communication> recordMap = new HashMap<String, Communication>();
	public List<Communication> all = new ArrayList<Communication>();
	public Communication root;
	
	
	private List<String> sqlForeignList = new ArrayList<String>();
	
	private boolean ready = true;
	

	// 叶子节点

	public List<Communication> leafList = new ArrayList<Communication>();
	
	
	
	public List<Communication> reqList = new ArrayList<Communication>();
	
	
	
	public void addReq(List<Communication> list) {
		for (Communication aCommunication : list) {
			reqList.add(aCommunication);
		}		
	}
	
	public Communication getTree(List<Communication> list) {
		this.all = list;
		for (Communication aCommunication : list) {
			recordMap.put("" + aCommunication.id, aCommunication);
			if (aCommunication.parent == 0) {
				root = aCommunication;
			}
		}
		for (Communication aCommunication : list) {
			String parentId = "" + aCommunication.parent;
			Communication parentNode = recordMap.get(parentId);
			aCommunication.setParent(parentNode);

		}

		extractLeafNode(root);
		return root;

	}

	/**
	 * 叶子节点
	 */
	private void extractLeafNode(Communication aCommunication) {
		List<Communication> list = new ArrayList<Communication>();
		if (aCommunication.hasChild()) {
			for (Communication bCommunication : aCommunication.children) {
				extractLeafNode(bCommunication);
			}
		} else {
			leafList.add(aCommunication);
		}
	}

	public boolean ready() {
		boolean flag = true;
		for (Communication aCommunication : all) {
			if (aCommunication.response == null || aCommunication.response.equalsIgnoreCase("")) {
				flag = false;
				break;
			}
		}
		return flag;
	}

	/**
	 * 初始化树
	 * Communication 与OperationMap一一对应
	 * OperationMap里有操作的SQL
	 * 得到外键缓存
	 * @param list
	 * @return
	 * Aug 31, 2010
	 */
	public Communication getInitTree(List<Communication> list) {
		this.all = list;
		RecordExtract aRecordExtract = new RecordExtract();
		for (Communication aCommunication : list) {
			aCommunication.convertOperationMap();
			recordMap.put("" + aCommunication.id, aCommunication);
			if (aCommunication.parent == 0) {
				root = aCommunication;
			}
			aCommunication.setTreeTool(this);
		}
		for (Communication aCommunication : list) {
			String parentId = "" + aCommunication.parent;
			Communication parentNode = recordMap.get(parentId);
			aCommunication.setParent(parentNode);
		}	
		return root;
	}
	
	
	
	public Map getNameID() throws Exception {
		Map aMap = new HashMap();
		for (Communication aCommunication : all) {
			aMap.put(aCommunication.getNameId().toUpperCase(), aCommunication);
		}		
		return aMap;
	}

	public List<String> getSqlForeignList() {
		return sqlForeignList;
	}

	public void setSqlForeignList(List<String> sqlForeignList) {
		this.sqlForeignList = sqlForeignList;
	}

	public void addForeignSQL(String sql) {
		this.sqlForeignList.add(sql);
	}
	
	
	
}
